package com.richmond.util;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;

/**
 * User: csavard
 * Date: 12-08-24
 * Time: 09:33
 */
public class ParserUtil {
    public static Long getLongValue(Cell cell, FormulaEvaluator evaluator) {
        Long retValue = null;
        Double doubleValue = getDoubleValue(cell, evaluator);
        if (doubleValue != null) {
            retValue = doubleValue.longValue();
        }

        return retValue;
    }

    public static double getDoubleValue(Cell cell, FormulaEvaluator evaluator) {
        Double retValue = 0.0;
        try {
            if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                retValue = Double.valueOf(cell.getStringCellValue());
            } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                retValue = cell.getNumericCellValue();
            } else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
                CellValue value = evaluator.evaluate(cell);
                if (value.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                    retValue = value.getNumberValue();
                } else if (value.getCellType() == Cell.CELL_TYPE_STRING) {
                    try {
                        retValue = Double.valueOf(value.getStringValue());
                    } catch (NumberFormatException e) {
                        //C<est possible que la string ne soit pas un number
                    }
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("Row: " + (cell.getRowIndex() + 1) + ", Column: " + (cell.getColumnIndex() + 1), e);
        }
        return retValue;
    }

    public static Float getFloatValue(Cell cell) {
        Float retValue = null;
        if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
            retValue = Float.valueOf(cell.getStringCellValue());
        } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
            retValue = ((Double) cell.getNumericCellValue()).floatValue();
        }

        return retValue;
    }

    public static String getStringValue(Cell cell, FormulaEvaluator evaluator) {
        String retValue = null;
        if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
            retValue = cell.getStringCellValue();
        } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
            retValue = String.valueOf(((Double) cell.getNumericCellValue()).longValue());
        } else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
            CellValue value = evaluator.evaluate(cell);
            if (value.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                retValue = String.valueOf(((Double) value.getNumberValue()).longValue());
            } else if (value.getCellType() == Cell.CELL_TYPE_STRING) {
                try {
                    retValue = value.getStringValue();
                } catch (NumberFormatException e) {
                    //C<est possible que la string ne soit pas un number
                }
            }
        }

        return retValue;
    }
}
